//
//  FLInterfaceManager+DuoBao.m
//  Pods
//
//  Created by LinXiaoBin on 16/3/21.
//
//

#import "FLInterfaceManager+Business.h"
#import "BusinessInterfaceDefine.h"
#import "FLInterfaceForBusiness.h"
#import <objc/runtime.h>

@implementation FLInterfaceManager(Business)

- (id<FLInterfaceBusinessParamDelegate>)businessParam
{
    id delegate = objc_getAssociatedObject(self, _cmd);
    // NSAssert(delegate != nil, @"参数代理未实现");
    return delegate;
}

- (void)setBusinessParam:(id<FLInterfaceBusinessParamDelegate>)businessParam {
    SEL key = @selector(businessParam);
    objc_setAssociatedObject(self, key, businessParam, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id<FLResponseSerializerDelegate>)businessResponseSerializer
{
    id delegate = objc_getAssociatedObject(self, _cmd);
    NSAssert(delegate != nil, @"response处理未实现");
    return delegate;
}

- (void)setBusinessResponseSerializer:(id<FLResponseSerializerDelegate>)businessResponseSerializer {
    SEL key = @selector(businessResponseSerializer);
    objc_setAssociatedObject(self, key, businessResponseSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - 业务接口

- (void)requestGetDatasWithPageIndex:(NSUInteger)PageIndex
                                   PageSize:(NSUInteger)PageSize
                               succeedBlock:(FLInterfaceSucceedBlock_t)succeedBlock
                                failedBlock:(FLInterfaceFailedBlock_t)failedBlock{
    FLInterfaceForBusiness *interface = [self felinkEmojiInterfaceWithSucceedBlock:succeedBlock failedBlock:failedBlock];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setObject:@(PageIndex) forKey:@"PageIndex"];
    [params setObject:@(PageSize) forKey:@"PageSize"];
    [interface requestAction:IRequestLastedResources params:params httpSendMethod:HttpSendMethodGET];
}

- (void)requestPostDatasWithPageIndex:(NSUInteger)PageIndex
                                   PageSize:(NSUInteger)PageSize
                               succeedBlock:(FLInterfaceSucceedBlock_t)succeedBlock
                                failedBlock:(FLInterfaceFailedBlock_t)failedBlock {
    FLInterfaceForBusiness *interface = [self felinkEmojiInterfaceWithSucceedBlock:succeedBlock failedBlock:failedBlock];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setObject:@(PageIndex) forKey:@"PageIndex"];
    [params setObject:@(PageSize) forKey:@"PageSize"];
    [interface requestAction:IRequestLastedResources params:params httpSendMethod:HttpSendMethodPOST];
}


/**
 *  @brief  资源上传生成url接口
 *
 *  @param uploadType       资源类型，参照枚举emojiUploadResourceType
 *  @param data          资源数据
 *  @param fileExt       资源后缀
 */
- (void)requestUploadResourcesWithData:(NSData *)data fileExt:(NSString *)fileExt succeedBlock:(FLInterfaceSucceedBlock_t)succeedBlock failedBlock:(FLInterfaceFailedBlock_t)failedBlock
{
    FLInterfaceForBusiness *interface = [self felinkEmojiInterfaceWithSucceedBlock:succeedBlock failedBlock:failedBlock];
    [interface requestAction:IRequestUploadResources params:@{
                                                            @"data": ValueOrEmpty(data),
                                                            @"fileext": ValueOrEmpty(fileExt)
                                                            } httpSendMethod:HttpSendMethodPOST];
}

#pragma mark - private
- (FLInterfaceForBusiness *)felinkEmojiInterfaceWithSucceedBlock:(FLInterfaceSucceedBlock_t)succeedBlock failedBlock:(FLInterfaceFailedBlock_t)failedBlock
{
    FLInterfaceForBusiness *interface = [[FLInterfaceForBusiness alloc] initWithSucceedBlock:succeedBlock failedBlock:failedBlock];
    interface.paramDelegate = self.businessParam;
    interface.responseSerializer = self.businessResponseSerializer;
    [self addInterface:interface];
    return interface;
}

- (NSInteger)userID {
    NSInteger userID = 0;
    id<FLInterfaceBusinessParamDelegate> paramDelegate = self.businessParam;
    if ([paramDelegate respondsToSelector:@selector(pUserID)]) {
        userID = [paramDelegate pUserID];
    }
    return userID;
}

- (NSString*)userName {
    NSString* userName = @"匿名用户";
    id<FLInterfaceBusinessParamDelegate> paramDelegate = self.businessParam;
    if ([paramDelegate respondsToSelector:@selector(pUserName)]) {
        userName = [paramDelegate pUserName];
    }
    return userName;
}

@end
